﻿using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.Rapor
{
    public partial class KartelaEtiketleri : DevExpress.XtraReports.UI.XtraReport
    {
        DataSet ds;
        int EmptyLabelCount;
        public bool Stop = false;
        public KartelaEtiketleri()
        {
            InitializeComponent();
        }

        public KartelaEtiketleri(int CustomerId, string productIds, int emptyLabelCount, bool IsCons, bool IsBarkod)
            : this()
        {
            EmptyLabelCount = emptyLabelCount;

            ds = Session.DataManager.ExecuteDataSet("GetKartelaEtiketi" + 
                (CustomerId == -2 ? "KST" : string.Empty), CustomerId, productIds);

            
            for (int i = 0; i < EmptyLabelCount; i++)
            {
                DataRow r= ds.Tables["Table"].NewRow();
                ds.Tables["Table"].Rows.InsertAt(r, 0);
            }


            this.DataMember = "Table";
            this.DataSource = ds;

            tcStokGrubu.DataBindings.Add("Text", null, "Table.StokGrup");
            tcKonstruksiyon.DataBindings.Add("Text", null, "Table.Konstruksiyon");
            tcFinish.DataBindings.Add("Text", null, "Table.Finish");
            tcFabricType.DataBindings.Add("Text", null, "Table.FabricType");
            tcMetraj.DataBindings.Add("Text", null, "Table.Metraj");
            tcKompozisyon.DataBindings.Add("Text", null, "Table.Kompozisyon");
            tcToplar.DataBindings.Add("Text", null, "Table.Toplar");
            xrBarCode1.DataBindings.Add("Text", null, "Table.ProductId");

            if (!IsCons)
            {
                trFinish.Dispose();
                trKonstruksiyon.Dispose();
            }

            if (!IsBarkod)
            {
                trBarkod.Dispose();
            }

            tcKompozisyon.BeforePrint += ReSize;
            tcKonstruksiyon.BeforePrint += ReSize;
            tcFinish.BeforePrint += ReSize;
            tcStokGrubu.BeforePrint += ReSize;
            tcFabricType.BeforePrint += ReSize;
            
        }       
        
        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            //float f = tbKumas.HeightF;
            EmptyLabelCount--;
        }


        private void ReSize(object sender, System.Drawing.Printing.PrintEventArgs e)
        {

            //Resize ı düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 10;

            XRLabel label = (XRLabel)sender;

            float newSize = 7.2F;
            Font defaultFont = new System.Drawing.Font("Calibri", newSize);

            float stringWidth = gr.MeasureString(label.Text, defaultFont).Width;
            float labelWidth = label.WidthF;
            
            if (labelWidth < stringWidth * factor)
                newSize = defaultFont.Size * label.WidthF / stringWidth / factor;


            label.Font = new Font(defaultFont.FontFamily, newSize);
            gr.Dispose();

            
        }

        

        private void xrTable2_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
            }
            
        }
        
        private void xrBarCode1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
            }
        }        
    }
}
